package com.chaosj.webmagic.convert;

import com.chaosj.webmagic.dto.SearchEnums;
import com.chaosj.webmagic.dto.SearchResponseDto;
import com.chaosj.webmagic.dto.idiomschina.LexIconResponseDto;
import us.codecraft.webmagic.Page;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

/**
 * @className: LexiconConvertHandler
 * @description:
 * @author: caoyangjie
 * @date: 2021/8/12
 **/
public abstract class LexiconConvertHandler<T extends SearchResponseDto> extends AbstractConvertPage<T>{
    protected static AtomicBoolean first = new AtomicBoolean(false);

    @Override
    public void convert(Page page) {
        if( first.compareAndSet(false, true) ){
            try{
                page.addTargetRequests(page.getHtml().xpath("//div[@class='service-content']//a/@href").all());
            } finally {
                first.set(true);
            }
        }else{
            SearchResponseDto lexicon = createOrGet(page);
            // 标题
            String title = page.getHtml().xpath("//div[@class='post-head']//a/text()").get();
            // 链接
            String target = holdTarget(page.getHtml().xpath("//div[@class='post-head']//a/@href").get());
            // 来源
            String source = page.getHtml().xpath("//div[@class='post-head']//span/text()").get();
            String content = page.getHtml().xpath("//div[@class='post-content']//p/text()").all().stream().collect(Collectors.joining("#"));
            lexicon.addObject(title, target, source, content);
        }
    }

    @Override
    public boolean support(SearchEnums instance) {
        return SearchEnums.LEXICON.equals(instance);
    }

    @Override
    public String holdTarget(String target) {
        return SearchEnums.LEXICON.getDomain()+target.substring(1);
    }

    @Override
    public void init(){
        first.set(false);
    }
}
